<?php
/*
 * *********************************************************
 * ## Module controllers must extend MD_Controller class ##
 * *********************************************************
 */
class Termination extends MD_Controller {
    
public function __construct()
{
    parent::__construct();

	/*
	 * ******************************************************************
	 * CHECK IF MODULE IS INSTALLED PROPERLY
	 * ******************************************************************
	 */
	$this->MODULE->config->load('module_config');	
	$short_name = $this->MODULE->config->item('short_name');
	$settings_table = $this->MODULE->config->item('settings_table');	 
	if(!$this->PACKAGE->security->app_modules->module_exist($short_name) || empty($short_name) || empty($settings_table))
	{
		$this->PACKAGE->errors->generate->code("001")->stop();
	}
    
            /*
             * ********************************************
             * LOAD LANGUAGE FILES FOR THIS MODULE ONLY
             * notice that some language files have been
             * loaded within MY_Controller core class
             * and those language files are common for all
             * modules
             * ********************************************
             */    
    
            $this->MODULE->lang->load('module_texts',LANGUAGE);
            
            /*
             * ********************************************
             * LOAD PACKAGES
             * ********************************************
             */
            $this->PACKAGE->core->packages->add('regional');   
			$this->PACKAGE->core->packages->add('routing');   
      
			/*
			 * ********************************************
			 * LOAD LIBRARIES
			 * ********************************************
			 */
			$this->PACKAGE->routing->load->library('carriers/carriers');

			$this->APPLICATION->load->library('app_carriers');
			$this->APPLICATION->load->library('app_trunks');
			$this->APPLICATION->load->library('app_codecs');
			
			$this->MODULE->load->library('module_trunks');			
			
	  
            /*
             * ********************************************
             * LOAD MODELS
             * ********************************************
             */
			$this->MODULE->load->model('module_mod_settings');
			
            $this->PACKAGE->regional->load->model('countries');
			$this->PACKAGE->regional->load->model('currencies');
            $this->PACKAGE->regional->load->model('timezone');
						
			$this->MODULE->load->model('module_mod_trunks');
			
			$this->PACKAGE->routing->load->library('carriers/carriers');
			$this->PACKAGE->routing->load->library('trunks/trunks');
			
			//$this->MODULE->load->library('module_translate');			           
}
    
public function index()
{
    /*
     * define data
     */          
    $data = array(
                    'title'       => lang('carriers_home_title'),
                    'description' => lang('carriers_home_desc'),
					'subpage' 	  => 'home',
					'trunk_type'  => 'termination'
                  );
          
    $data['countries'] = $this->PACKAGE->regional->countries->get_countries();

	if(!$this->input->post('data'))
	{
		$this->PACKAGE->routing->trunks->order_by = array(
															'column' => 'carrier_username',
															'direction' => 'ASC'
														 );
	
		// form not submitted - get all routes
		$data['trunks'] = $this->PACKAGE->routing->trunks->get_all_trunks("termination");
    }
	else
	{
		$this->MODULE->module_mod_trunks->order_by = array(
															'column' => 'carrier_id',
															'direction' => 'DESC'
														 );

		// form submitted - get routes according to posted filters
		$data['trunks'] = $this->MODULE->module_mod_trunks->search_trunks($this->input->post('data'),"termination");
		
		// prepare page data
		$data_new = $this->input->post('data');
		
		$data = array_merge($data,$data_new);
	}
	
    /*
     * parse templates
     */
    
	$data['this'] = $this;
	
    $this->load->view('header',$data); // shared header
    
    $this->MODULE->load->view('module_header'); // module header    
    
    $this->load->view('topmenu'); // theme sample template          
    
    $this->MODULE->load->view('module_termination_home',$data); // theme sample template              
    
    $this->MODULE->load->view('module_footer'); // module footer
     
    $this->load->view('footer'); // shared footer
    
    
}


public function add()
{
    /*
     * define data
     */          
    $data = array(
                    'title'       => lang('carriers_home_title'),
                    'description' => lang('carriers_home_desc'),
					'subpage' 	  => 'add',
					'trunk_type'  => 'termination'
                  );
				  
    $data['countries'] = $this->PACKAGE->regional->countries->get_countries();	
          
    /*
     * parse templates
     */
    
	$data['this'] = $this;
	
    $this->load->view('header',$data); // shared header
    
    $this->MODULE->load->view('module_header'); // module header    
    
    $this->load->view('topmenu'); // theme sample template          
    
    $this->MODULE->load->view('module_termination_add',$data); // theme sample template              
    
    $this->MODULE->load->view('module_footer'); // module footer
     
    $this->load->view('footer'); // shared footer
    
    
}

public function edit($trunk_id)
{
    /*
     * define data
     */          
    $data = array(
                    'title'       => lang('carriers_home_title'),
                    'description' => lang('carriers_home_desc'),
					'subpage' 	  => 'add',
					'trunk_type'  => 'termination'
                  );
				  
    $data['countries'] = $this->PACKAGE->regional->countries->get_countries();	
	
	$data['trunk'] = $this->PACKAGE->routing->trunks->get_trunk_as_array_by_id($trunk_id,"termination");				  
          
    /*
     * parse templates
     */
    
	$data['this'] = $this;
	
    $this->load->view('header',$data); // shared header
    
    $this->MODULE->load->view('module_header'); // module header    
    
    $this->load->view('topmenu'); // theme sample template          
    
    $this->MODULE->load->view('module_termination_edit',$data); // theme sample template              
    
    $this->MODULE->load->view('module_footer'); // module footer
     
    $this->load->view('footer'); // shared footer
    
    
}

}


?>
